<?php

/**
 * MVM_MALL 网上商店系统 flash轮转广告
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-09-05 $
 * $Id: cycle.inc.php  www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if ($action=='list') {
    /*flash广告*/
    include_once MVMMALL_ROOT.'./config/flash_cyle.php';
    @extract($cycle);
      $result = $db->query("SELECT * FROM `{$tablepre}cycle` WHERE type_img='0'");
      while ($rt = $db->fetch_array($result)){
          $rt['del']  = "admincp.php?module=$module&action=del&focus_width=$focus_width&focus_height=$focus_height&text_height=$text_height&uid=$rt[uid]";
          $photo[]   = $rt;
      }
      include template('cycle');
      footer();
}elseif ($action=='add'){
    //图片
      if($_FILES['gallery']['name'][0]!=''){
        require_once MVMMALL_ROOT.'./include/upfile.class.php';
        $f = new upfile('jpg','./images/banner/');
        foreach ($_FILES['gallery']['name'] AS $key => $value){
            $upload = array(
            'name' => $_FILES['gallery']['name'][$key],
            'type' => $_FILES['gallery']['type'][$key],
            'tmp_name' => $_FILES['gallery']['tmp_name'][$key],
            'error' => $_FILES['gallery']['error'][$key],
            'size' => $_FILES['gallery']['size'][$key],
            );
            $img = $f->upload($upload);
            $title = dhtmlchars($title[$key]);
            $link = dhtmlchars($link[$key]);
            $rows = array(
                          'title'=> $title,
                          'link' =>$link,
                          'img'=> $img
                          );
            $db->insert("{$tablepre}cycle",$rows);
        }
    }
     cycle($focus_width,$focus_height,$text_height);
    admin_msg('success',dreferer());
    //end
}
//删除图片
elseif ($action=='del' && is_numeric($uid)){
    $rt_img = $db->get_one("SELECT img,uid FROM `{$tablepre}cycle` WHERE type_img='0' AND uid='$uid'");
    file_unlink($rt_img['img']);
    $db->query("DELETE FROM `{$tablepre}cycle` WHERE type_img='0' AND uid='$uid'");
    cycle($focus_width,$focus_height,$text_height); 
}
//修改标题和连接
elseif ($action=='edit' && is_numeric($uid)){
    if (isset($title)){
        $title = iconv("gb2312", "UTF-8",$title);
        $rows  = array('title'=>$title);
    }else {
        $rows  = array('link'=>$link);
    }
   $rows = dhtmlchars($rows);
   $db->update("{$tablepre}cycle",$rows,$where="uid='$uid'");
   cycle($focus_width,$focus_height,$text_height); 
}
  else {
    admin_msg('pass_worng');
}
//生成乱转广告列表
function cycle($focus_width,$focus_height,$text_height)
{
    global $tablepre,$db,$user_url;
     $result = $db->query("SELECT * FROM `{$tablepre}cycle` WHERE type_img='0'");
      while ($list=$db->fetch_array($result)){
          $pics[]  = $list['img'];
          $links[] = $list['link'];
          $texts[] = $list['title'];
      }
      $pics  = implode('+|+',$pics);
      $links = implode('+|+',$links);
      $texts = implode('+|+',$texts);
    $array   = array(
                  'focus_width' =>$focus_width,
                  'focus_height'=>$focus_height,
                  'text_height' => $text_height,
                  'swf_height'  => ($focus_height+$text_height),
                  'pics'        =>$pics,
                  'links'       =>$links,
                  'texts'       =>$texts
                  );
    $data = "<?PHP\n \$cycle= ". var_export(dhtmlchars($array), true) .";\n";
    writeover('./config/flash_cyle.php',$data);
}